//
//  HMNetworkingTools.swift
//  webo
//
//  Created by apple on 2016/11/15.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking
//声明枚举
enum HttpMethod: Int{
    case GET = 0
    case POST
    
}




class HMNetworkingTools: AFHTTPSessionManager {

    static let shared: HMNetworkingTools = {
    let tools = HMNetworkingTools()
  tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()
    
    //声明封装的函数
    /*
     1.请求方法
     2.urlString
     3.参数
     4.请求的回调
     - 成功
     - 失败
     */
    func request(method: HttpMethod,urlString: String,parameter: Any?, finishedBlock: @escaping (Any? , Error?) -> ()){
        
//        let successCallBlock = { (task: URLSessionDataTask, response: Any?) -> () in
//            finishedBlock(response,nil)
//            print(response)
//        }
//        let errorBlock = { (task: URLSessionDataTask?,error: Error) -> () in
//            finishedBlock(nil,error)
//        }
//        
//        
//        if method == .GET {
//            self.get(urlString, parameters: parameter, progress: nil, success: successCallBlock, failure: errorBlock)
//        }else{
//            
//            self.post(urlString, parameters: parameter, progress: nil, success: successCallBlock, failure: errorBlock)
//        }
        let successCallBack = { (task: URLSessionDataTask,responseObject: Any?) -> () in
            //这里需要使用到参数
            finishedBlock(responseObject, nil)
        }
        
        //失败的回调
        let failureCallBack = { (task: URLSessionDataTask?, error: Error) -> () in
            //输出错误信息
            print(error)
            finishedBlock(nil, error)
        }
        
        if method == .GET {
            self.get(urlString, parameters: parameter, progress: nil, success: successCallBack, failure: failureCallBack)
        } else {
            self.post(urlString, parameters: parameter, progress: nil, success:successCallBack, failure: failureCallBack)
        }
        
    }
    
    
    
    
}
